package com.tojabo.swing.eight_list;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

public class ListModelDemo1 {
	public ListModelDemo1() {
		JFrame f = new JFrame("JList");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = f.getContentPane();
		ListModel mode = new DataModel();
		JList list = new JList(mode);// 利用ListModel建立一个JList.
		list.setVisibleRowCount(5);// 设置程序一打开时所能看到的数据项个数。
		list.setBorder(BorderFactory.createTitledBorder("配置一台电脑需要的组件"));

		contentPane.add(new JScrollPane(list));
		f.pack();

		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new ListModelDemo1();
	}
}

class DataModel extends AbstractListModel// 创建一个类，实现抽象类AbstractListModel，这个类用来创建一个列表框
{
	String[] s = { "主板", "显示器", "内存", "CPU", "硬盘", "电源", "键盘", "鼠标" };

	public Object getElementAt(int index) {
		return (index + 1) + "." + s[index++];
	}

	public int getSize() {
		return s.length;
	}
}
